use "data.dta",clear
global control Size Lev Age Roa Cflow Invt Soe    
global fe id year
global vce vce(robust)
xtset id year
***===Table 1
asdoc sum ESG Pub_data $control, stat(N mean sd min max) dec(2) save(summary stats1.doc) title(Table1) replace

***===Table 2
reghdfe ESG Pub_data ,noabsorb  keepsingletons  $vce 
est store m1
reghdfe ESG Pub_data ,absorb(year) keepsingletons  $vce 
est store m2
reghdfe ESG Pub_data ,absorb($fe) keepsingletons  $vce 
est store m3
reghdfe ESG Pub_data $control,noabsorb  keepsingletons  $vce
est store m4
reghdfe ESG Pub_data $control,absorb(year) keepsingletons  $vce 
est store m5
reghdfe ESG Pub_data $control,absorb($fe) keepsingletons  $vce 
est store m6
esttab m1 m2 m3 m4 m5 m6, replace b(%6.3f) t(%6.2f) r2 nogap star(* 0.1  ** 0.05  *** 0.01)
esttab m1 m2 m3 m4 m5 m6 using Table2.rtf, replace b(%6.4f) keep(Pub_data $control _cons) se(%6.4f) r2(%6.4f) nogap star(* 0.1  ** 0.05  *** 0.01) 

***===Table 3 and Figure 3
gen distance_data=year-data_year
 gen d0  = 0 
 replace d0  = 1 if data_year!=. & distance_data==0
  foreach i of numlist 1/10 {
capture gen d_`i'=0
 replace d_`i'=1 if data_year!=. & distance_data== -`i'
 } 
  foreach i of numlist 1/10 {
 capture gen d`i'=0
 replace d`i'=1 if data_year!=. & distance_data== `i'
 } 
  reghdfe ESG d_1-d_9 d0 d1-d10,absorb($fe) keepsingletons $vce 
  est store m1
  reghdfe ESG d_1-d_9 d0 d1-d10 $control,absorb($fe) keepsingletons $vce 
  est store m2
esttab m1 m2,replace b(%6.3f) t(%6.2f) r2 nogap star(* 0.1  ** 0.05  *** 0.01)
esttab m1 m2 using Table3.rtf, replace b(%6.4f)   se(%6.4f) r2(%6.4f) nogap star(* 0.1  ** 0.05  *** 0.01)
   
gen t = invttail(27736,0.05)   
gen b0 = _b[d0] 
gen se_b0 = _se[d0]
gen b0LB = b0 - t * se_b0
gen b0UB = b0 + t * se_b0

 foreach i of numlist 1/9 {
 	gen b_`i' = _b[d_`i'] 
 	gen se_b_`i' = _se[d_`i']
 	gen b_`i'LB = b_`i' - t * se_b_`i'
 	gen b_`i'UB = b_`i' + t * se_b_`i'
 }

 foreach i of numlist 1/10 {
 	gen b`i' = _b[d`i'] 
 	gen se_b`i' = _se[d`i']
 	gen b`i'LB = b`i' - t * se_b`i'
 	gen b`i'UB = b`i' + t * se_b`i'
 }

gen b = .
gen LB = .
gen UB = .
 foreach i of numlist 1/9 {
replace b = b_`i'  if distance == -`i'
replace LB = b_`i'LB if distance == -`i'
replace UB = b_`i'UB if distance == -`i'
 }
 foreach i of numlist 1/10 {
replace b = b`i'  if distance == `i'
replace LB = b`i'LB if distance == `i'
replace UB = b`i'UB if distance == `i'
 }

replace b = b0  if distance == 0
replace LB = b0LB if distance == 0
replace UB = b0UB if distance == 0
preserve
keep distance_data b LB UB  
duplicates drop distance_data,force
sort distance_data  
drop if distance_data==-10

set scheme s1mono
twoway (connected b distance_data) (rcap LB UB distance_data, lcolor(navy) lpattern(dash) msize(medium)) 

graph save Figure3,replace  //Figure 3
restore  

***===Table 4, Table 5, and Figure 4
preserve
use "data.dta", clear
 gen treat=(data_year!=.)
  set seed 101
 gen tmp=runiform()
 sort tmp
 psmatch2 treat $control , out(ESG) logit neighbor(1) common ate 
  pstest $control, both graph graphregion(fcolor(white)) //Figure4
 gen psm_sample1=1 if _weight!=.
 reghdfe ESG Pub_data  if psm_sample1==1,absorb($fe) keepsingletons
 est store m1
 reghdfe ESG Pub_data $control if psm_sample1==1,absorb($fe) keepsingletons
 est store m2
restore


preserve
use "data.dta", clear
  gen treat=(data_year!=.)
ebalance treat $control, tar(2) // Table 4
reghdfe ESG Pub_data  [aw=_webal],absorb($fe) keepsingletons 
est store m3
reghdfe ESG Pub_data  $control [aw=_webal],absorb($fe) keepsingletons
est store m4
esttab m1 m2 m3 m4, se r2 star(* 0.1  ** 0.05  *** 0.01)  
esttab m1 m2 m3 m4 using Table5.rtf, replace b(%6.4f) keep(Pub_data _cons) se(%6.4f) r2(%6.4f) nogap star(* 0.1  ** 0.05  *** 0.01) 
restore

***===Figure 5
preserve
permute Pub_data beta = _b[Pub_data], reps(500) seed(100)  saving("Simulations.dta"): reghdfe ESG Pub_data $control,absorb($fe) keepsingletons $vce
use Simulations,clear
#delimit ;
dpplot beta, xline(0.0408, lc(black*0.5) lp(dash))
             xline(0, lc(black*0.5) lp(solid))
             xtitle("Estimator", size(*0.8)) 
             xlabel(-0.05(0.01)0.05, labsize(small))
             ytitle("Density", size(*0.8)) 
             ylabel(, nogrid format(%4.1f) labsize(small)) 
             note("") caption("") 
             graphregion(fcolor(white)) ;             
#delimit cr  //Figure 5
restore

***===Table 6
fect ESG, treat(Pub_data) unit(id) time(year)  method("fe") seed(30) lambda(0.001) se nboots(100) 
mat list e(ATT)  //column (1) in Table 6
fect ESG, treat(Pub_data) unit(id) time(year)  method("fe") seed(30) lambda(0.001) se nboots(100) cov($control) 
mat list e(ATT) //column (2) in Table 6

***===Table 7
did_imputation ESG id year data_year,allhorizons pretrends(9) minn(0) autosample controls($control) fe($fe)  tol(1) maxit(100)   
est store Borusyak
did_imputation ESG id year data_year, autosample controls($control) fe($fe)  tol(1) maxit(100)   //see tau

  gen nevertreated = (data_year==.) 
gen distance_data=year-data_year
 
  foreach i of numlist 1/10 {
 capture gen qian_`i'=0
 replace qian_`i'=1 if data_year!=. & distance_data== -`i'
 } 

  foreach i of numlist 0/10 {
 capture gen hou_`i'=0
 replace hou_`i'=1 if data_year!=. & distance_data== `i'
 } 

preserve
  drop qian_1
 eventstudyinteract ESG qian_* hou_*, cohort(data_year) control_cohort(nevertreated)  covariates($control) absorb($fe) vce (cluster id)
 est store SA
   matrix sa_b = e(b_iw) // storing the estimates for later
   matrix sa_v = e(V_iw)
   ereturn post sa_b sa_v
   lincom(hou_0+hou_1+hou_2+hou_3+hou_4+hou_5+hou_6+hou_7+hou_8+hou_9+hou_10)/11 //column (2) in Table 7
  restore   

preserve 
  drop qian_1
stackedev ESG  qian_* hou_*,cohort(data_year) time(year) never_treat(nevertreated) unit_fe(id) clust_unit(id) covariates($control)
 est store stackedev 
 lincom(hou_0+hou_1+hou_2+hou_3+hou_4+hou_5+hou_6+hou_7+hou_8+hou_9+hou_10)/11 // column (3) in Table 7
  restore   

 ***===Table 8 
reghdfe Pub_data IV1 $control,absorb($fe)  keepsingletons $vce
  est store m1
ivreghdfe ESG (Pub_data = IV1) $control,absorb($fe)  $vce keepsingletons
  est store m2
reghdfe  Pub_data IV2 $control,absorb($fe)   $vce keepsingletons 
  est store m3
ivreghdfe ESG (Pub_data = IV2) $control,absorb($fe)  $vce keepsingletons
  est store m4
esttab m1 m2 m3 m4, replace b(%6.3f) t(%6.2f) r2 nogap star(* 0.1  ** 0.05  *** 0.01) 
esttab m1 m2 m3 m4 using Table8.rtf, replace b(%6.4f) keep(Pub_data IV1  IV2 _cons) se(%6.4f) r2(%6.4f) nogap star(* 0.1  ** 0.05  *** 0.01)  

***===Table 9
global Y ESG
global X Size Lev Age Roa Cflow Invt Soe i.id i.year
global D Pub_data
set seed 42 
ddml init partial, kfolds(4) 
ddml E[D|X]: pystacked $D $X, type(reg) method(rf) 
ddml E[Y|X]: pystacked $Y $X, type(reg) method(rf) 
ddml crossfit 
ddml estimate, cluster(id) //column (1) in Table 9

set seed 42 
ddml init partial, kfolds(4) 
ddml E[D|X]: pystacked $D $X, type(reg) method(lassocv) 
ddml E[Y|X]: pystacked $Y $X, type(reg) method(lassocv) 
ddml crossfit 
ddml estimate, cluster(id) //column (2) in Table 9

set seed 42 
ddml init interactive, kfolds(4)
ddml E[Y|X,D]: pystacked $Y $X, type(reg) method(rf) 
ddml E[D|X]: pystacked $D $X, type(reg) method(rf) 
ddml crossfit 
ddml estimate, cluster(id) //column (3) in Table 9

***===Table 10
  reghdfe ESG Pub_data Dig_infra $control,absorb($fe) keepsingletons  $vce 
  est store m1
  reghdfe ESG Pub_data Data_trade $control,absorb($fe) keepsingletons  $vce 
  est store m2
  reghdfe ESG Pub_data Gov_infor $control,absorb($fe) keepsingletons  $vce 
  est store m3
  esttab m1 m2 m3, replace b(%6.3f) t(%6.2f) r2 nogap star(* 0.1  ** 0.05  *** 0.01)
   esttab m1 m2 m3 using Table10.rtf, replace b(%6.4f) keep(Pub_data  _cons) se(%6.4f) r2(%6.4f) nogap star(* 0.1  ** 0.05  *** 0.01) 

***===Table 11
  reghdfe ESG_Score Pub_data ,absorb($fe) keepsingletons  $vce 
  est store m1
  esttab m1, replace b(%6.3f) t(%6.2f) r2 nogap star(* 0.1  ** 0.05  *** 0.01)
  reg ESG data_index $control if year==2021, cluster(id) r 
  est store m2
  reg ESG data_index $control if year==2022, cluster(id) r 
  est store m3
  esttab m1 m2 m3 , replace b(%6.3f) t(%6.2f) r2 nogap star(* 0.1  ** 0.05  *** 0.01)
  esttab m1 m2 m3 using Table11.rtf, replace b(%6.4f) keep(Pub_data data_index _cons) se(%6.4f) r2(%6.4f) nogap star(* 0.1  ** 0.05  *** 0.01) 
   
***===Table 12
reghdfe ESG Pub_data $control,absorb($fe) keepsingletons  $vce 
est store m1
reghdfe Inf_asy Pub_data $control,absorb($fe) keepsingletons  $vce 
est store m2
reghdfe ESG Pub_data Inf_asy $control,absorb($fe) keepsingletons  $vce 
est store m3
esttab m1 m2 m3, replace b(%6.3f) t(%6.2f) r2 nogap star(* 0.1  ** 0.05  *** 0.01)
esttab m1 m2 m3 using Table12.rtf, replace b(%6.4f) keep(Pub_data Inf_asy _cons) se(%6.4f) r2(%6.4f) nogap star(* 0.1  ** 0.05  *** 0.01) 

***===Table 13
reghdfe ESG Pub_data $control,absorb($fe) keepsingletons $vce 
est store m1
reghdfe Dig_tran Pub_data $control,absorb($fe) keepsingletons $vce
est store m2
reghdfe ESG Pub_data Dig_tran $control,absorb($fe) keepsingletons $vce 
est store m3
esttab m1 m2 m3, replace b(%6.3f) t(%6.2f) r2 nogap star(* 0.1  ** 0.05  *** 0.01)
esttab m1 m2 m3 using Table13.rtf, replace b(%6.4f) keep(Pub_data Dig_tran _cons) se(%6.4f) r2(%6.4f) nogap star(* 0.1  ** 0.05  *** 0.01) 

***===Table 14
reghdfe ESG Pub_data $control,absorb($fe) keepsingletons $vce 
est store m1
reghdfe Gre_inno Pub_data $control,absorb($fe) keepsingletons  $vce 
est store m2
reghdfe ESG Pub_data Gre_inno $control,absorb($fe) keepsingletons $vce 
est store m3
esttab m1 m2 m3, replace b(%6.3f) t(%6.2f) r2 nogap star(* 0.1  ** 0.05  *** 0.01)
esttab m1 m2 m3 using Table14.rtf, replace b(%6.4f) keep(Pub_data Gre_inno _cons) se(%6.4f) r2(%6.4f) nogap star(* 0.1  ** 0.05  *** 0.01) 

***===Table 15
egen mean_index=median(Infor_index)
reghdfe ESG Pub_data $control if Infor_index>=mean_index,absorb($fe) keepsingletons $vce 
est store m1
reghdfe ESG Pub_data $control if Infor_index<mean_index,absorb($fe) keepsingletons  $vce 
est store m2
esttab m1 m2, replace b(%6.3f) t(%6.2f) r2 nogap star(* 0.1  ** 0.05  *** 0.01)

egen mean_market=median(market)
reghdfe ESG Pub_data $control if market>=mean_market,absorb($fe) keepsingletons $vce 
est store m3
reghdfe ESG Pub_data $control if market<mean_market,absorb($fe) keepsingletons $vce
est store m4
esttab m1 m2 m3 m4, replace b(%6.3f) t(%6.2f) r2 nogap star(* 0.1  ** 0.05  *** 0.01)
esttab m1 m2 m3 m4 using Table15.rtf, replace b(%6.4f) keep(Pub_data  _cons) se(%6.4f) r2(%6.4f) nogap star(* 0.1  ** 0.05  *** 0.01) 

***===Table 16
egen mean_FEPU=median(fepu_sentence)
reghdfe ESG Pub_data $control if fepu_sentence>=mean_FEPU,absorb($fe) keepsingletons $vce 
est store m1
reghdfe ESG Pub_data $control if fepu_sentence<mean_FEPU,absorb($fe) keepsingletons $vce
est store m2

reghdfe ESG c.Pub_data $control if Soe==1,absorb($fe) keepsingletons $vce 
est store m3
reghdfe ESG c.Pub_data $control if Soe==0,absorb($fe) keepsingletons $vce 
est store m4
esttab m1 m2 m3 m4, replace b(%6.3f) t(%6.2f) r2 nogap star(* 0.1  ** 0.05  *** 0.01)
esttab m1 m2 m3 m4 using Table16.rtf, replace b(%6.4f) keep(Pub_data _cons) se(%6.4f) r2(%6.4f) nogap star(* 0.1  ** 0.05  *** 0.01) 
